const Web3 = require('web3');
const web3 = new Web3('http://127.0.0.1:7545');
// 假设 flcomABI 是你的合约 ABI
import { flcomABI } from './flcomABI';
// 合约配置
const CONTRACT_ADDRESS = '0x95e33f2c9345d7DBCF1C5C025d6d5b82DC792c5F';
const PRIVATE_KEY = '0xf036b61e74ba1f64fcc62c9f7c1b1cda2544a84b1fb4fa8ca820279a1e42951c';
const ABI = flcomABI; // 合约 ABI

// 创建合约实例
const contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS);

// 账户处理
const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
web3.eth.defaultAccount = account.address;

// 获取记录
export const getcomRecord = async (taskId, isServer, trainingRound) => {
    try {
        const result = await contract.methods.getRecord(taskId, isServer, trainingRound).call();
        const blockNumber = await web3.eth.getBlockNumber();
        const blockInfo = await web3.eth.getBlock(blockNumber);

        return {
            success: true,
            data: {
                taskId: result.taskId,
                weight: result.weight,
                isServer: result.isServer,
                trainingRound: result.trainingRound,
                blockNumber: blockNumber,
                hash: blockInfo.hash,
                timestamp: blockInfo.timestamp,
                transactionCount: blockInfo.transactions.length
            }
        };
    } catch (error) {
        return { success: false, error: error.message };
    }
};

// 上传记录
export const uploadcomRecord = async (taskId, weight, isServer, trainingRound) => {
    try {
        const nonce = await web3.eth.getTransactionCount(account.address);
        const gasPrice = await web3.eth.getGasPrice();

        const txData = contract.methods.uploadRecord(taskId, weight, isServer, trainingRound).encodeABI();

        const tx = {
            from: account.address,
            to: CONTRACT_ADDRESS,
            gas: 2000000,
            gasPrice,
            nonce,
            data: txData
        };

        const signedTx = await account.signTransaction(tx);
        const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);

        return {
            success: true,
            txHash: receipt.transactionHash,
            blockNumber: receipt.blockNumber
        };
    } catch (error) {
        return { success: false, error: error.message };
    }
};

// 获取区块和合约信息
export const getcomBlockAndContractInfo = async (blockNumber) => {
    try {
        const blockInfo = await web3.eth.getBlock(blockNumber);

        return {
            success: true,
            block: {
                hash: blockInfo.hash,
                timestamp: blockInfo.timestamp,
                transactionCount: blockInfo.transactions.length
            }
        };
    } catch (error) {
        return { success: false, error: error.message };
    }
};

export const getcomLatestBlockInfo = async () => {
    try {
        const latestBlock = await web3.eth.getBlock('latest');
        if (!latestBlock) {
            throw new Error('Failed to retrieve the latest block.');
        }

        return {
            success: true,
            data: {
                blockNumber: latestBlock.number,
                hash: latestBlock.hash,
                timestamp: latestBlock.timestamp,
                transactionCount: latestBlock.transactions.length,
                miner: latestBlock.miner,
                difficulty: latestBlock.difficulty,
                size: latestBlock.size
            }
        };
    } catch (error) {
        return { success: false, error: error.message };
    }
};    